package net.avh4.scratch.rpg;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;

import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;

public class WorldMapTestbed extends Canvas {

	/**
	 * 1L: initial version 
	 */
	private static final long serialVersionUID = 1L;
	
	private WorldMap map;
	private Avatar avatar;
	private Keyboard k = null;
	private Component up;
	private Component down;
	private Component left;
	private Component right;
	
	public WorldMapTestbed(WorldMap map, Avatar avatar) {
		setMap(map);
		this.avatar = avatar;
		
		ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
		Controller[] controllers = ce.getControllers();
		for (int i = 0; i < controllers.length; i++) {
			if (controllers[i].getType() == Controller.Type.KEYBOARD) {
				k = (Keyboard) controllers[i];
			}
		}
		up = k.getComponent(Component.Identifier.Key.UP);
		down = k.getComponent(Component.Identifier.Key.DOWN);
		left = k.getComponent(Component.Identifier.Key.LEFT);
		right = k.getComponent(Component.Identifier.Key.RIGHT);
	}
	
	public void setMap(WorldMap map) {
		if (map == null) {
			throw new IllegalArgumentException("Map must not be null");
		}
		this.map = map;
		
		if (this.isVisible()) {
			this.repaint();
		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		try {
			WorldMapTestbed testbed = new WorldMapTestbed(new RandomWalkWorldMap(),
					new Avatar("images/avatar-s.png"));
			frame.getContentPane().add(testbed);
			frame.setSize(640, 480);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
			testbed.runGame();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void runGame() {
		Thread thread = new Thread() {

			@Override
			public void run() {
				while (true) {
					try {
						sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					k.poll();
					if (up.getPollData() > 0.5) {
						avatarPoint.y -= 1;
					} else if (down.getPollData() > 0.5) {
						avatarPoint.y += 1;	
					} else if (left.getPollData() > 0.5) {
						avatarPoint.x -= 1;
					} else if (right.getPollData() > 0.5) {
						avatarPoint.x += 1;
					}
					WorldMapTestbed.this.repaint();
				}
			}
			
		};
		thread.start();
	}
	
	private Point avatarPoint = new Point(20,15);

	@Override
	public void paint(Graphics g) {
		BufferedImage buffer = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
		assert map != null;
		final int tileWidth = 16;
		final int tileHeight = 16;
		final Graphics2D g2 = buffer.createGraphics();
		for (int y = 0, tileY = -10+avatarPoint.y; y < this.getHeight(); y += tileHeight, tileY++) {
			for (int x = 0, tileX = -10+avatarPoint.x; x < this.getWidth(); x += tileWidth, tileX++) {
				map.getTerrain(tileX, tileY).draw(g2, x, y, x+tileWidth, y+tileHeight);
			}
		}
		avatar.draw(g2, 10 * tileWidth, 10 * tileHeight);
		
		g.drawImage(buffer, 0, 0, null);
	}

}
